package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Dish;
import com.itheima.dto.DishDto;
import com.itheima.bean.PageParams;
import com.itheima.common.R;
import com.itheima.exception.MyException;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author hebing
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService ds;

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        //调用service进行添加操作
        ds.addDish(dishDto);
        return R.success("添加菜品成功");
    }

    /**
     * 分页查询
     * @param params
     * @return
     */
    @GetMapping("/page")
    public R<IPage<DishDto>> findPage(PageParams params){
        try {
            //调用service层查询
            IPage<DishDto> page=ds.findPage(params);
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败");
        }
    }

    /**
     * 修改
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        System.out.println(dishDto);
        //调用service进行修改数据
        int rows = ds.update(dishDto);
        //判定
        if(rows>0){
            return R.success("修改成功");
        }else{
            return R.error("修改失败");
        }
    }

    //根据菜品id查询有哪些菜品
    @GetMapping("/list")
    public R getDish(Long categoryId){
        try {
            //调用service查询湘菜有哪些菜品
            List<DishDto> list=ds.getDish(categoryId);
            //返回结果对象
            return R.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }

    /**
     * 删除&批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteByIds(Long[] ids){
        //调用service从数据库中进行删除
        int rows=ds.deleteByIds(ids);
        if(rows>0){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    /**
     * 批量起售
     * @return
     */
    @PostMapping("/status/1")
    public R updateStatusToOn(@RequestParam List<Long> ids){
        //调用service进行删除
        int rows=ds.updateStatusToOn(ids);
        //判定
        if(rows>0){
            return R.success("批量操作成功");
        }else {
           throw new MyException("状态无需更改");
        }
    }

    /**
     * 批量停售
     * @return
     */
    @PostMapping("/status/0")
    public R updateStatusToOff(@RequestParam List<Long> ids){
        //调用service进行删除
        int rows=ds.updateStatusToOff(ids);
        //判定
        if(rows>0){
            return R.success("批量操作成功");
        }else {
            throw new MyException("状态无需更改");
        }
    }
}
